<template>
    <div class="serviceHistoey">
        <!-- 最近服务历史查询 -->
        <!-- <PageTitle pageTitle="最近服务历史查询" /> -->
        <div class="formContainer">
            <!-- 搜索表单模块 -->
            <el-form
                class="searchForm searchForm_inline"
                ref="searchForms"
                :model="searchForm"
                :inline="true"
                label-position="right"
                size="small"
                label-width="110px"
                :rules="rules"
            >
                <el-row>
                    <el-col :span="7">
                        <el-form-item prop="agentNo" label="坐席工号:">
                            <el-input placeholder="请输入工号" v-model="searchForm.agentNo"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="cardNo" label="卡号:">
                            <el-input placeholder="请输入银行卡号" v-model="searchForm.cardNo"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="certificate" label="证件号:">
                            <el-input placeholder="请输入证件号码" v-model="searchForm.certificate"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="custName" label="客户名称:">
                            <el-input placeholder="请输入客户名称" v-model="searchForm.custName"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item prop="createTimeStart" label="开始日期:">
                            <el-date-picker
                                v-model="searchForm.createTimeStart"
                                type="datetime"
                                placeholder="请选择开始日期"
                                format="yyyy-MM-dd HH:mm:ss"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                :picker-options="startOptions"
                                @change="startTimeChange"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item prop="createTimeEnd" label="结束日期:">
                            <el-date-picker
                                v-model="searchForm.createTimeEnd"
                                type="datetime"
                                placeholder="请选择结束日期"
                                format="yyyy-MM-dd HH:mm:ss"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                :picker-options="endtOptions"
                                @change="endTimeChange"
                            ></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7" :style="{textAlign: 'right',marginBottom:'5px'}">
                        <el-button
                            type="primary"
                            size="small"
                            icon="el-icon-search"
                            :loading="searchLoading"
                            @click="handleSearch"
                        >查 询</el-button>
                        <el-button
                            type="primary"
                            size="small"
                            icon="el-icon-refresh-right"
                            @click="handleReset"
                        >重 置</el-button>
                        <el-button type="primary" size="small" @click="handleExport">导 出</el-button>
                    </el-col>
                </el-row>
            </el-form>
            <!-- 表格数据展示模块 -->
            <el-table :data="tableData" stripe border class="heard-bg">
                <!-- <el-table-column
                    v-for="(item, index) in tableColumn"
                    :key="index"
                    :label="item.label"
                    :min-width="item.minWidth"
                >
                    <template slot-scope="scope">
                        <span
                            v-if="item.value === 'certificateType'"
                        >{{ scope.row.certificateType | getText(certificateTypeList)}}</span>
                        <span v-else-if="item.value === 'operate'">
                            <el-button
                                size="small"
                                type="text"
                                @click="handleTurnPage(scope.row)"
                            >创建工单</el-button>
                        </span>
                        <span v-else>{{scope.row[item.value]}}</span>
                    </template>
                </el-table-column>-->

                <el-table-column label="服务历史表">
                    <el-table-column
                        :label="`表格时间：${tableHeader.startTime}-${tableHeader.endTime}`"
                    >
                        <el-table-column label="坐席工号" width="120px">
                            <template slot-scope="scope">{{scope.row.agentNo}}</template>
                        </el-table-column>
                        <el-table-column label="服务名称">
                            <template slot-scope="scope">{{scope.row.businessName}}</template>
                        </el-table-column>
                        <el-table-column label="会话ID">
                            <template slot-scope="scope">{{scope.row.callId}}</template>
                        </el-table-column>
                        <el-table-column label="卡号">
                            <template slot-scope="scope">{{scope.row.cardNo}}</template>
                        </el-table-column>
                        <el-table-column label="证件类型" width="140px">
                            <template
                                slot-scope="scope"
                            >{{scope.row.certificateType | getText(certificateTypeList)}}</template>
                        </el-table-column>
                        <el-table-column label="证件号">
                            <template slot-scope="scope">{{scope.row.certificate}}</template>
                        </el-table-column>
                        <el-table-column label="客户名称" width="140px">
                            <template slot-scope="scope">{{scope.row.custName}}</template>
                        </el-table-column>
                    </el-table-column>
                    <el-table-column :label="`制表日期：${tableHeader.makeTime}`">
                        <el-table-column label="手机号" width="140px">
                            <template slot-scope="scope">{{scope.row.phone}}</template>
                        </el-table-column>
                        <el-table-column label="创建时间" width="160px">
                            <template slot-scope="scope">{{scope.row.createTime}}</template>
                        </el-table-column>
                    </el-table-column>
                </el-table-column>
            </el-table>

            <Pagination
                :hanleChangeCurrentPage="hanleChangeCurrentPage"
                :handleChangeSize="handleChangeSize"
                :total="total"
                :pageSize="pageSize"
                :currentPage="currentPage"
            />
        </div>
    </div>
</template>

<script>
import moment from 'moment';
import Pagination from '@/components/Pagination/Pagination.vue';
import { getDictSelectList, isNull } from '@/utils/Common';
import PageTitle from '@/components/pageTitle/PageTitle.vue';
import request from '@/apis/request';
import { eachProp } from '@/utils/Object';


export default {
    name: 'serviceHistoey',
    components: {
        Pagination,
        PageTitle,
    },
    data() {
        return {
            currentPage: 1, // 当前分页
            pageSize: 10, // 每页数量
            total: 0, // 数据总数
            searchLoading: false, // 查询按钮loading
            // 证件类型
            certificateTypeList: [],
            searchForm: { // 搜索表单字段
                agentNo: '', // 坐席工号
                bankNum: '', // 银行号
                businessName: '', // 业务名称
                callId: '', // 会话id
                cardNo: '', // 卡号
                certificateType: '', // 证件类型
                certificate: '', // 证件号码
                createTimeStart: '', // 开始时间
                createTimeEnd: '', // 结束时间
                custName: '', // 客户名称
                menuName: '', // 菜单名称
                phone: '', // 客户电话
            },
            // 表格展示项
            tableColumn: [
                {
                    label: '坐席工号',
                    value: 'agentNo',
                    minWidth: '6%',
                },
                {
                    label: '服务名称',
                    value: 'businessName',
                    minWidth: '10%',
                },
                {
                    label: '会话ID',
                    value: 'callId',
                    minWidth: '12%',
                },
                {
                    label: '卡号',
                    value: 'cardNo',
                    minWidth: '10%',
                },
                {
                    label: '证件类型',
                    value: 'certificateType',
                    minWidth: '7%',
                },
                {
                    label: '证件号',
                    value: 'certificate',
                    minWidth: '12%',
                },
                {
                    label: '客户名称',
                    value: 'custName',
                    minWidth: '8%',
                },
                {
                    label: '手机号',
                    value: 'phone',
                    minWidth: '10%',
                },
                {
                    label: '创建时间',
                    value: 'createTime',
                    minWidth: '10%',
                },
                // {
                //     label: '操作',
                //     value: 'operate',
                //     minWidth: '8%',
                // },
            ],
            // 表格数据
            tableData: [{}],
            rules: {},
            // 开始日期选择 限制
            startOptions: {
                disabledDate: (time) => {
                    if (this.searchForm.createTimeEnd) {
                        return time.getTime() > new Date(moment(this.searchForm.createTimeEnd).format('YYYY-MM-DD HH:mm:ss')).getTime();
                    }
                },
            },
            // 结束日期选择限制
            endtOptions: {
                disabledDate: (time) => {
                    if (this.searchForm.createTimeStart) {
                        return time.getTime() < new Date(moment(this.searchForm.createTimeStart).format('YYYY-MM-DD HH:mm:ss')).getTime();
                    }
                },
            },
            tableHeader: { // 表头显示
                startTime: '', //moment(new Date(new Date().getTime() - (1000 * 60 * 60 * 24 * 30))).format('YYYY-MM-DD HH:mm:ss'), // 开始日期 默认为当前日期往前一个月
                endTime: '', //moment(new Date()).format('YYYY年MM月DD日hh时mm分'), // 截止时间 默认为当前日期
                makeTime: '', //moment(new Date()).format('YYYY年MM月DD日'), //制表时间
            },
        };
    },
    created() { },
    mounted() {
        this.searchForm.createTimeStart = moment(new Date(new Date().getTime() - (1000 * 60 * 60 * 24 * 30))).format('YYYY-MM-DD HH:mm:ss');
        this.searchForm.createTimeEnd = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
        this.tableHeader = { // 表头显示
            startTime: moment(new Date(new Date().getTime() - (1000 * 60 * 60 * 24 * 30))).format('YYYY年MM月DD日HH时mm分'), // 开始日期 默认为当前日期往前一个月
            endTime: moment(new Date()).format('YYYY年MM月DD日HH时mm分'), // 截止时间 默认为当前日期
            makeTime: moment(new Date()).format('YYYY年MM月DD日'), //制表时间
        };
        // 初始化方法
        this.init();
    },

    filters: {
        // 证件类型转译
        getText(val, list) {
            let text = '';
            if (val) {
                list.forEach((item) => {
                    if (item.code === val) {
                        text = item.label;
                    }
                });
            }
            return text;
        },
    },
    methods: {

        async init() {
            // 获取证件类型下拉选择列表
            this.certificateTypeList = await getDictSelectList('IDTYPE', this);

            // 默认查询
            this.getListData();
        },

        startTimeChange() {
            this.tableHeader.startTime = moment(this.searchForm.createTimeStart).format('YYYY年MM月DD日HH时mm分') //查询开始时间
        },

        endTimeChange() {
            this.tableHeader.endTime = moment(this.searchForm.createTimeEnd).format('YYYY年MM月DD日HH时mm分'); // 查询结束时间
        },

        // 获取表格数据
        getListData() {
            const params = {
                ...this.searchForm,
                pageNum: this.currentPage,
                pageSize: this.pageSize,
            };

            // 查询接口
            this.$post.businessStatistics.getLatestHistory(params).then((res) => {
                console.log(res);
                if (res.status == 200 && res.data.success) {
                    this.tableData = res.data.content.recordList;
                    this.total = res.data.content.recordCount;
                    // if (this.tableData.length) {
                    //     this.$message.success('查询成功');
                    // } else {
                    //     this.$alert(res.data.msg || '当前查询条件无数据！', '提示', {
                    //         customClass: 'warning', type: 'warning',
                    //     });
                    // }
                } else {
                    this.$message.error('查询失败', '提示');
                }
            });
        },
        // 点击导出 -> 导出流水记录表格
        handleExport() {
            const data = {
                ...this.searchForm,
            };
            let url = '/iccsrv/optTrail/downloadServeHistory?';
            eachProp(data, (prop) => {
                if (!isNull(data[prop])) {
                    url += `${prop}=${data[prop].toString()}&`;
                }
            });
            url = url.substring(0, url.length - 1);
            request({
                url,
                method: 'get',
                responseType: 'blob',
            }).then((res) => {
                // console.log(res, '相应报文')
                const blobUrl = window.URL.createObjectURL(new Blob([res.data]));
                const a = document.createElement('a');
                a.style.display = 'none';
                a.download = `服务历史表-${moment(new Date()).format('YYYYMMDDHHmmss')}.xls`;
                a.href = blobUrl;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
            });
        },
        // 点击查询
        handleSearch() {
            this.currentPage = 1;
            this.getListData();
        },

        // 点击重置搜索表单
        handleReset() {
            this.$refs.searchForms.resetFields();
            this.currentPage = 1;
            this.getListData();
        },

        // 切换分页大小
        handleChangeSize(val) {
            this.pageSize = val;
            // 重置页码
            this.currentPage = 1;
            this.getListData();
        },

        // 切换页码
        hanleChangeCurrentPage(val) {
            this.currentPage = val;
            this.getListData();
        },

        // 点击跳转页面
        handleTurnPage(params) {
            // console.log(params, '23423423423');
            // this.$store.commit('UPDATE_SERVICE_DATA', params)
            sessionStorage.setItem('serviceData', JSON.stringify(params));
            this.$router.push({
                name: 'information',
            });
        },
    },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.serviceHistoey {
    padding: 20px;
    height: 100%;
    background-color: #fff;

    .formContainer .el-input__inner {
        width: 100%;
        /* max-width: 200px; */
    }
}
</style>
